import count from '@dfeidao/atom-nodejs/db/count';
import operate from '@dfeidao/atom-nodejs/db/operate';
// import log from '@dfeidao/atom-nodejs/logger/log';
import doc_code from '@dfeidao/atom-nodejs/msg/doc-code';
import sys_date from '@dfeidao/atom-nodejs/msg/sys-date';
import { v4 as uuid } from 'uuid';
import { systemid } from '../../atom/config';
interface Message {
	productid: string;
	level_name: string;
}

export default async function atom(msg: Message, action_id: string, session_id: string) {
	// const doc_code1 = await doc_code(action_id, session_id, systemid, '12', 'BH013', 'TL', 1);
	const doc_code1 = await doc_code(action_id, session_id, systemid, 'BH013', 'TL', 1);
	const level_no1 = doc_code1.result;
	const systime = await sys_date(action_id, session_id);
	const { prepare, exec } = count(action_id, session_id, '12');
	const { insert, exec: e } = operate(action_id, session_id, '12');
	prepare('tenant_level', { productid: msg.productid, level_name: msg.level_name });
	prepare('tenant_level', { productid: msg.productid });
	const [cou, coun1] = await exec();
	if (cou === 0) {
		insert('tenant_level', { _id: uuid(), productid: msg.productid, level_name: msg.level_name, level_no: level_no1, create_time: systime, order_num: coun1 + 1 });
		const ret = await e();
		if (ret.code === 1) {
			return 'success';
		} else {
			return 'fail';
		}
	} else {
		return 'toomuch';
	}
}
